-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Parse triple quoted string annotations as if parenthesized #15387
Conversation
b33cf05
to
efbd25e
Compare
|
88460a5
to
be3115f
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you!! The red-knot test and changes look fine to me, but that's a small part of this PR. Would rather have @dhruvmanila take a look at the parser/ruff side.
crates/red_knot_python_semantic/resources/mdtest/annotations/string.md
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you. Would you mind adding a test where an expression is incorrectly parenthesized?
def f(
a: """
int |
str)
"""
)
or
def f(
a: """
int) |
str
"""
)
I'm curious how it recovers in those cases and if we need to do something more than just setting nesting = 1
93125a2
to
b3cf440
Compare
@MichaReiser I added the test cases you suggested and the tests pass(see this commit) So I fixed the nesting check to check for nesting value of 1 when mode is |
# single quotes are not implicitly parenthesized | ||
invalid: "\n int" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we add the same test case using triple-quoted string which will be useful as a documentation?
# but, using the same annotation inside a triple-quoted string is valid
valid: """\n int"""
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually I made a mistake here, invalid: "\n int"
is in a python file. So this string will not actually have a newline character but \
and n
as characters. So also """"\n int"""
fails.
Is there a way to actually create a type annotation that contains newline inside a python file without using triple quotes?
https://pyright-play.net/?code=JYOwbghgNsAmBcACARKgOiRoAurkCh9RIYEVUiRcKg
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I removed this right now. Since I could not figure out how to do it correctly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see, that makes sense. I don't think it's even possible to have a newline in a single-quoted string without the \n
character. This will also utilize parse_complex_annotation
and not parse_simple_annotation
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks. This looks great.
Co-authored-by: Dhruv Manilawala <[email protected]>
Co-authored-by: Dhruv Manilawala <[email protected]>
Co-authored-by: Micha Reiser <[email protected]>
cf342b5
to
a74d41e
Compare
F722.py:30:11: F722 Syntax error in forward annotation: ` | ||
int | | ||
str) | ||
` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unrelated to this PR, I think it would be useful to provide the syntax error that was raised during parsing this string content.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for working on this!
## Summary Ref: #15387 (comment) This PR updates `F722` to show syntax error message instead of the string content. I think it's more useful to show the syntax error message than the string content. In the future, when the diagnostics renderer is more capable, we could even highlight the exact location of the syntax error along with the annotation string. This is also in line with how we show the diagnostic in red knot. ## Test Plan Update existing test snapshots.
Summary
Resolves #9467
Parse quoted annotations as if the string content is inside parenthesis.
With this logic
x
andy
in this example are equal:Also this rule only applies to triple quotes(link).
This PR is based on the comments on the issue.
I did one extra change, since we don't want any indentation tokens I am setting the
State::Other
as the initial state of the Lexer.Remaining work:
Test Plan
Added a test which previously failed because quoted annotation contained indentation.
Added an mdtest for red-knot.
Updated previous test.